home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / midas060 / src / mpoll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-26  |  4.6 KB  |  211 lines

  1. /*      mpoll.c
  2.  *
  3.  * Functions for polling MIDAS Sound System in a thread
  4.  *
  5.  * $Id: mpoll.c,v 1.9 1997/01/26 23:29:45 jpaana Exp $
  6.  *
  7.  * Copyright 1996,1997 Housemarque Inc.
  8.  *
  9.  * This file is part of the MIDAS Sound System, and may only be
  10.  * used, modified and distributed under the terms of the MIDAS
  11.  * Sound System license, LICENSE.TXT. By continuing to use,
  12.  * modify or distribute this file you indicate that you have
  13.  * read the license and understand and accept it fully.
  14. */
  15.  
  16. #ifndef __LINUX__
  17. #define WIN32_LEAN_AND_MEAN
  18. #include <windows.h>
  19. #include <process.h>
  20. #else
  21. #include <unistd.h>
  22. #include "pthread.h"
  23. #endif
  24. #include "midas.h"
  25.  
  26. RCSID(const char *mpoll_rcsid = "$Id: mpoll.c,v 1.9 1997/01/26 23:29:45 jpaana Exp $";)
  27.  
  28. #ifdef __LINUX__
  29.     static pthread_t pollThread = NULL;
  30. #endif
  31.  
  32.  
  33.  
  34. /****************************************************************************\
  35. *
  36. * Function:     void PollMIDAS(void)
  37. *
  38. * Description:  Polls MIDAS Sound System
  39. *
  40. \****************************************************************************/
  41.  
  42. void PollMIDAS(void)
  43. {
  44.     int         error;
  45.     int         callMP;
  46.  
  47.     if ( !midasSDInit )
  48.         return;
  49.  
  50.     if ( (error = midasSD->StartPlay()) != OK )
  51.         midasError(error);
  52.     do
  53.     {
  54.         if ( (error = midasSD->Play(&callMP)) != OK )
  55.             midasError(error);
  56.         if ( callMP )
  57.         {
  58.             if ( midasGMPInit )
  59.             {
  60.                 if ( (error = gmpPlay()) != OK )
  61.                     midasError(error);
  62.             }
  63.         }
  64.     } while ( callMP && (midasSD->tempoPoll == 0) );
  65. }
  66.  
  67.  
  68.  
  69. static volatile int stopPolling = 0;
  70. static volatile unsigned pollSleep = 50;
  71.  
  72. #ifdef __LINUX__
  73. void *PollerThread(void *dummy)
  74. #else
  75. #ifdef __WC32__
  76. static void PollerThread(void *dummy)
  77. #else
  78. static void __cdecl PollerThread(void *dummy)
  79. #endif
  80. #endif
  81. {
  82.     dummy = dummy;
  83.  
  84. #ifndef __LINUX__
  85.     /* We'd better make the player thread's priority still above normal: */
  86.     SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
  87. #endif
  88.  
  89.     while ( stopPolling == 0 )
  90.     {
  91.         PollMIDAS();
  92. #ifdef __LINUX__
  93.     usleep(pollSleep * 1000);
  94. #else
  95.         Sleep(pollSleep);
  96. #endif
  97.     }
  98.  
  99.     stopPolling = 0;
  100.  
  101. #ifdef __LINUX__
  102.     pthread_exit(0);
  103.     return NULL;
  104. #else
  105.     _endthread();
  106. #endif
  107. }
  108.  
  109.  
  110.  
  111. /****************************************************************************\
  112. *
  113. * Function:     void StartPlayThread(unsigned pollPeriod)
  114. *
  115. * Description:  Starts polling MIDAS in a thread
  116. *
  117. * Input:        unsigned pollPeriod     polling period (delay between two
  118. *                                       polling loops) in milliseconds
  119. *
  120. \****************************************************************************/
  121.  
  122. void StartPlayThread(unsigned pollPeriod)
  123. {
  124. #ifndef __LINUX__
  125. #ifdef __WC32__
  126.     int         pollThread;
  127. #else
  128.     unsigned long pollThread;
  129. #endif
  130. #else
  131.     int code;
  132. #endif
  133.     pollSleep = pollPeriod;
  134.  
  135.     /* Start polling MIDAS in a thread: */
  136. #ifdef __LINUX__
  137.     code = pthread_create(&pollThread, NULL, PollerThread, NULL);
  138.     if ( code )
  139.         midasErrorExit("StartPlayThread: Couldn't create player thread!");
  140. #else
  141. #ifdef __WC32__
  142.     pollThread = _beginthread(PollerThread, NULL, 4096, NULL);
  143.     if ( pollThread == -1 )
  144.         midasErrorExit("StartPlayThread: Couldn't create player thread!");
  145. #else
  146.     pollThread = _beginthread(PollerThread, 4096, NULL);
  147.     if ( pollThread == -1 )
  148.         midasErrorExit("StartPlayThread: Couldn't create player thread!");
  149. #endif
  150. #endif
  151. }
  152.  
  153.  
  154.  
  155.  
  156. /****************************************************************************\
  157. *
  158. * Function:     void StopPlayThread(void)
  159. *
  160. * Description:  Stops polling MIDAS in a thread
  161. *
  162. \****************************************************************************/
  163.  
  164. void StopPlayThread(void)
  165. {
  166. #ifdef __LINUX__
  167.     void    *retval;
  168. #endif
  169.     /* Ugly but works */
  170.  
  171.     stopPolling = 1;
  172.     while ( stopPolling )
  173. #ifdef __LINUX__
  174.         pthread_join(pollThread, &retval);
  175. #else
  176.         Sleep(pollSleep/2);
  177. #endif
  178. }
  179.  
  180.  
  181. /*
  182.  * $Log: mpoll.c,v $
  183.  * Revision 1.9  1997/01/26 23:29:45  jpaana
  184.  * Small fixes for Linux
  185.  *
  186.  * Revision 1.8  1997/01/16 18:41:59  pekangas
  187.  * Changed copyright messages to Housemarque
  188.  *
  189.  * Revision 1.7  1996/09/28 09:00:16  jpaana
  190.  * Still fixes for Linux
  191.  *
  192.  * Revision 1.6  1996/09/28 08:12:40  jpaana
  193.  * Fixed return value of pthread_create
  194.  *
  195.  * Revision 1.5  1996/09/28 06:50:36  jpaana
  196.  * Fixed for Linuxthreads-0.4
  197.  *
  198.  * Revision 1.4  1996/09/21 17:40:45  jpaana
  199.  * fixed some warnings
  200.  *
  201.  * Revision 1.3  1996/09/21 17:18:01  jpaana
  202.  * Added Linux-stuff
  203.  *
  204.  * Revision 1.2  1996/09/02 20:19:30  pekangas
  205.  * Changed to use _beginthread with Visual C as well
  206.  *
  207.  * Revision 1.1  1996/08/06 20:36:37  pekangas
  208.  * Initial revision
  209.  *
  210. */
  211.